<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="未入库商品明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <purchaseNotMx ref="purchaseNotMx"></purchaseNotMx>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="confirmBtn"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
      
      <script>
import VolBox from "@/components/basic/VolBox.vue";
import purchaseNotMx from "@/views/purchaseinfo/purchase_not_mx/q_purchase_not_mx.vue";
import { ElMessage } from "element-plus";
export default {
  components: {
    "vol-box": VolBox,
    purchaseNotMx,
  },
  data() {
    return {
      model: false,
      WHSE_Id:null,
    };
  },
  methods: {
    async onModelClose() {
      // console.log("关闭");
      this.$refs.purchaseNotMx.searchFormFields.采购单号 = null;
      this.$refs.purchaseNotMx.searchFormFields.供应商 = null;
      this.$refs.purchaseNotMx.searchFormFields.编码 = null;
      this.$refs.purchaseNotMx.searchFormFields.名称 = null;
    },
    async open(Supplier,WHSE_Id) {
      this.WHSE_Id = WHSE_Id;
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.purchaseNotMx) {
          clearInterval(setIntervalTiem);
          this.$refs.purchaseNotMx.searchFormFields.供应商 = Supplier;
          this.$refs.purchaseNotMx.$refs.grid.search();
        }
      }, 100);
    },
    async confirmBtn() {
      let rowObj = this.$refs.purchaseNotMx.$refs.grid.getSelectRows();
      if (!rowObj.length)
        return this.base.ElMessageFn("请至少选择一条单据", "warning");
      const Supplier = this.$refs.purchaseNotMx.searchFormFields.供应商;
      let list = await rowObj.filter(
        (item) => item.入库状态 == 0 && item.供应商 == Supplier
      ); //未入库
      let list2 = await rowObj.filter((item) => item.入库状态 != 0); //非未入库
      if (list2.length) {
        let str = "";
        list2.forEach((r) => {
          if (str == "") str += r.名称;
          else str = str + "," + r.名称;
        });
        this.$message.warning(`商品${str}非未入库`);
      }
      let list3 = await rowObj.filter((item) => item.供应商 != Supplier); //非当前供货商
      if (list3.length) {
        let str = "";
        list3.forEach((r) => {
          if (str == "") str += r.名称;
          else str = str + "," + r.名称;
        });
        this.$message.warning(`商品${str}非当前供应商`);
      }
      this.$emit("parentCall", ($parent) => {
        try {
          //处理明细表已含有相同数据
          let parentList = $parent.$refs.detail.rowData;
          let set = new Set(parentList.map((item) => item.Purchase_ID)); //得到由明细表goods_sub组成的数组
          let commonObjects = list.filter((item) => set.has(item.ID)); //得到明细表中已包含的选中行
          let goodsNamesSet = new Set(commonObjects.map((item) => item.ID)); //相同商品的编码
          if (commonObjects.length) {
            list = list.filter((item) => !goodsNamesSet.has(item.ID)); //在选中数据中剔除明细表已包含的行数据
            let str = "";
            commonObjects.forEach((r) => {
              if (str == "") str += r.名称;
              else str = str + "," + r.名称;
            });
            this.$message.warning(`商品${str}已存在此入库单`);
          }
          let newRowObj = list.map((item) => {
            console.log(item)
            return {
              Purchase_ID: item.ID,
              Voucher_sub: item.采购单号,
              Cost_price: item.单价,
              Tax_rate: item.税率,
              Goods_num: item.数量,
              Goods_size: item.规格,
              Total_price: item.总价,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Tax_exclusive: item.无税单价,
              Tax_exclusive_total_price: item.无税总价,
              WHSE_name: item.库位名称,
            };
          });
          newRowObj = this.base.getPushDatileWHSEName(newRowObj, "Goods_sub",this.WHSE_Id);
          $parent.$refs.detail.rowData.unshift(...newRowObj);
        } catch (error) {
          console.error(error);
        }
      });
      this.onModelClose();
      return (this.model = false);
    },
    ElMessageFn(title) {
      ElMessage.closeAll();
      ElMessage({
        message: title,
        type: "warning",
      });
    },
    //明细表库位下拉框
    // async getDatileSelectFn($parent, sub) {
    //   let rows = $parent.$refs.detail.rowData;
    //     await rows.forEach((row) => {
    //     $parent.$refs.detail.columns.forEach((item) => {
    //       if (item.field == "WHSE_name") {
    //         // this.base.getWHSEName(item,row[sub])
    //         let selectUrl = `api/q_warehouse_locationList/getgoodslocation?bm=${row[sub]}`;
    //         item.bind.data.forEach((d) => {
    //           d.hidden = true;
    //         });
    //         this.http.post(selectUrl, {}, "").then((reslut) => {
    //           item.bind.data.forEach((d) => {
    //             d.hidden = true;
    //           });
    //           item.bind.data.forEach((d) => {
    //             reslut.forEach((j) => {
    //               if (d.key == j.WHSE_no) {
    //                 d.hidden = false;
    //               }
    //             });
    //           });
    //           row.WHSE_name = item.bind.data.filter(
    //             (x) => x.hidden == false
    //           )[0].key;
    //         });
    //       }
    //     });
    //   });

    // },
  },
};
</script>
      
      <style>
</style>